﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
using CWB.Common.Logging;
using CWB.Common.Constant;

namespace CWB.Common.Utilities
{
    public class Localization
    {
        private static readonly ISysLog Log = SysLog.GetLogger(typeof(Localization));

        public static string Localize(string key)
        {
            string value = default(string);
            try
            {
                value = (string)HttpContext.GetGlobalResourceObject("Text", key, Thread.CurrentThread.CurrentUICulture);
            }
            catch
            {
                Log.Exception(string.Format("Key [{0}] not found.", key));
                value = key;
            }
            if (string.IsNullOrEmpty(value))
            {
                value = key;
            }
            return value;
        }

        public static string TranslateMessage(string msg, MessageType mt)
        {
            if (!string.IsNullOrEmpty(msg) && !msg.Contains(' '))
            {
                string prefix = default(string);
                long errornumber = GetLong(msg);
                if (errornumber > 0)
                {
                    switch (mt)
                    {
                        case MessageType.Error:
                            prefix = "err";
                            break;
                        case MessageType.Message:
                            prefix = "msg";
                            break;
                    }
                    msg = Localization.Localize(string.Format("{0}{1:0000}", prefix, errornumber));
                }
            }
            return msg;
        }

        public static string TranslateMessage(Enum msgEnum)
        {
            string msg = string.Empty;
            if (msgEnum.GetType() == typeof(Msg))
            {
                msg = Localization.Localize(string.Format("{0}{1:0000}", "msg", (int)(Msg)msgEnum));
            }
            else if (msgEnum.GetType() == typeof(Err))
            {
                msg = Localization.Localize(string.Format("{0}{1:0000}", "err", (int)(Err)msgEnum));
            }
            else
            {
                msg = Localization.Localize(string.Format("{0}{1:0000}", "err", msgEnum.ToString()));
            }
            return msg;
        }


        /*
        public static string TrandlateMessage(int id, MessageType mt)
        {
            string globalKey = string.Format("{0:0000}", id);
            switch (mt)
            {
                case MessageType.Error:
                    globalKey = string.Format("Err{0}", globalKey);
                    break;
                case MessageType.Message:
                    globalKey = string.Format("Msg{0}", globalKey);
                    break;
            }
            return Localization.Localize(globalKey);
        }
        */

        public static string TrandlateMessage(object o)
        {
            string key = string.Empty;
            if (o.GetType() == typeof(Msg))
            {
                key = string.Format("Msg{0:0000}", (int)(Msg)o);
            }
            else if (o.GetType() == typeof(CWB.Common.Constant.Err))
            {
                key = string.Format("Err{0:0000}", (int)(Err)o);
            }
            else
            {
                return o.ToString();
            }
            return Localization.Localize(key);
        }

        private static long GetLong(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                try
                {
                    return Convert.ToInt64(message);
                }
                catch
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

    }
}
